﻿using System;
using System.Windows.Forms;

namespace Z.IconLibraryGenerator.WinForm
{
    public class Default : Page
    {
        #region Desginer

        /// <summary>The menu bar.</summary>
        public MenuBar MenuBar;

        /// <summary>The project new.</summary>
        public HtmlElement uiProjectNew;

        /// <summary>The project open.</summary>
        public HtmlElement uiProjectOpen;

        /// <summary>The project recent.</summary>
        public HtmlElement uiProjectRecent;

        /// <summary>The web browser control.</summary>
        public WebBrowser uiWebBrowser;

        #endregion

        /// <summary>Constructor.</summary>
        /// <param name="webBrowser">The web browser control.</param>
        public Default(WebBrowser webBrowser)
        {
            try
            {
                MenuBar = new MenuBar(webBrowser);

                uiWebBrowser = webBrowser;


                if (uiWebBrowser.Document == null)
                {
                    throw new Exception("Document cannot be null.");
                }

                uiProjectNew = uiWebBrowser.Document.GetElementById("uiProjectNew");
                uiProjectOpen = uiWebBrowser.Document.GetElementById("uiProjectOpen");
                uiProjectRecent = uiWebBrowser.Document.GetElementById("uiProjectRecent");

                BindEvents();
                LoadProjectRecent();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"General Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>Bind events.</summary>
        public void BindEvents()
        {
            uiProjectNew.Click += ProjectNew_OnClick;
            uiProjectOpen.Click += ProjectOpen_OnClick;
        }

        /// <summary>Loads project recent.</summary>
        public void LoadProjectRecent()
        {
            if (uiWebBrowser.Document == null)
            {
                throw new Exception("Document cannot be null.");
            }

            foreach (ApplicationProject project in My.ApplicationConfiguration.Projects)
            {
                HtmlElement div = uiWebBrowser.Document.CreateElement("div");

                if (div == null)
                {
                    throw new Exception("Failed to create DIV element.");
                }

                string html = string.Format("<span class='glyphicon glyphicon-file'></span><span class='projectrecent-file'>&nbsp;{0}</span>", project.ProjectName);
                div.InnerHtml = html;

                div.Click += (sender, args) => OpenProject(project.ProjectPath);

                uiProjectRecent.AppendChild(div);
            }
        }

        /// <summary>Event handler. Called by ProjectNew for on click events.</summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">HTML element event information.</param>
        public void ProjectNew_OnClick(object sender, HtmlElementEventArgs e)
        {
            My.CurrentProject = new ProjectConfiguration();
            uiWebBrowser.Url = new Uri(My.Url.ProjectSettings);
        }

        /// <summary>Event handler. Called by ProjectOpen for on click events.</summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">HTML element event information.</param>
        public void ProjectOpen_OnClick(object sender, HtmlElementEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = @"Icon Generator Project File (*.zicongen)|*.zicongen|All files (*.*)|*.*",
                InitialDirectory = @"C:\",
                Title = @"Open a Icon Library Generator Project File"
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string path = dialog.FileName;
                OpenProject(path);
            }
        }

        /// <summary>Opens a project.</summary>
        /// <param name="projectPath">Full pathname of the project file.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        public bool OpenProject(string projectPath)
        {
            string path = projectPath;

            ProjectConfiguration projectConfiguration = ProjectConfiguration.Load(path);

            if (projectConfiguration == null)
            {
                MessageBox.Show(@"Could not load the IconLibrary Generator project file. The file may be corrupted with invalid data.", @"Could not open file", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                My.CurrentProject = projectConfiguration;
                uiWebBrowser.Url = new Uri(My.Url.ProjectSettings);
                return true;
            }

            return false;
        }
    }
}